﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MLManagement.Models;

namespace MLManagement.Controllers
{
    public class CustomerController : Controller
    {
        private MLManagementDbContext db = new MLManagementDbContext();

        #region Index
        //
        // GET: /Customer/

        //        public ViewResult Index()
        //        {
        //            ViewBag.IsCustomerSearchOn = false;
        //#if DEBUG == true
        //            return View(new List<Customer> { 
        //                new Customer {   Country = "Argentina",
        //                                 CustomerID = 1,
        //                                 Email="hernan.zbucki@gmail.com",
        //                                 FirstName="Hernan",
        //                                 LastName="Zbucki",
        //                                 Locality="CABA",
        //                                 MlUser="CEZBU",
        //                                 MobilePhone="1164529330",
        //                                 Phone="20534439",
        //                                 Province="CABA"                                 
        //                              }, 
        //                 new Customer {  Country = "España",
        //                                 CustomerID = 2,
        //                                 Email="pepe@gmail.com",
        //                                 FirstName="Pepe",
        //                                 LastName="Guardiola",
        //                                 Locality="Barcelona",
        //                                 MlUser="Guard",
        //                                 MobilePhone="1155447845",
        //                                 Phone="124455",
        //                                 Province="Barcelona"                                 
        //                              }
        //            });
        //#else
        //            return View(db.Customers.ToList());
        //#endif
        //        }

        public ViewResult Index(string mod)
        {
            Session["AreCustomersSelectable"]= (mod == "selectOneCust");
            ViewBag.AreCustomersSelectable = Session["AreCustomersSelectable"];

#if DEBUG == true
            return View(new List<Customer> { 
                new Customer {   Country = "Argentina",
                                 CustomerID = 1,
                                 Email="hernan.zbucki@gmail.com",
                                 FirstName="Hernan",
                                 LastName="Zbucki",
                                 Locality="CABA",
                                 MlUser="CEZBU",
                                 MobilePhone="1164529330",
                                 Phone="20534439",
                                 Province="CABA"                                 
                              }, 
                 new Customer {  Country = "España",
                                 CustomerID = 2,
                                 Email="pepe@gmail.com",
                                 FirstName="Pepe",
                                 LastName="Guardiola",
                                 Locality="Barcelona",
                                 MlUser="Guard",
                                 MobilePhone="1155447845",
                                 Phone="124455",
                                 Province="Barcelona"                                 
                              }
            });
#else
            return View(db.Customers.ToList());
#endif
        }

        // POST: /Customer/
        [HttpPost]
        public ActionResult Index(string submit, string searchCriteria)
        {
            ViewBag.AreCustomersSelectable = Session["AreCustomersSelectable"];

            switch (submit)
            {
                case "Buscar Cliente":
#if DEBUG == true
                    return View(new List<Customer> { 
                            new Customer {   Country = "Argentina",
                                             CustomerID = 1,
                                             Email="hernan.zbucki@gmail.com",
                                             FirstName="Hernan",
                                             LastName="Zbucki",
                                             Locality="CABA",
                                             MlUser="CEZBU",
                                             MobilePhone="1164529330",
                                             Phone="20534439",
                                             Province="CABA"                                 
                                          }
                        });
#else
                    var query = from c in db.Customers
                                where (((String.IsNullOrEmpty(c.FirstName) == false) ? c.FirstName : String.Empty) +
                                                            ((String.IsNullOrEmpty(c.LastName) == false) ? c.LastName : String.Empty) +
                                                            ((String.IsNullOrEmpty(c.MlUser) == false) ? c.MlUser : String.Empty)
                                                         ).Contains(searchCriteria)
                                select c;

                    return View(query.AsEnumerable().ToList());
#endif
                default:
                    return RedirectToAction("Create");
            }

        }
        #endregion
        #region Details - DEPRECATED
        //
        // GET: /Customer/Details/5

        public ViewResult Details(int id)
        {
#if DEBUG == true
            return View(new Customer
            {
                Country = "Argentina",
                CustomerID = id,
                Email = "mail@mail.com",
                FirstName = "Custy",
                LastName = "Merr",
                Locality = "NoWhereLand",
                MlUser = "MLUser",
                MobilePhone = "54122435",
                Phone = "1234548",
                Province = "NoProvince"

            });
#else
            Customer customer = db.Customers.Find(id);
            return View(customer);
#endif
        }
        #endregion
        #region Create
        //
        // GET: /Customer/Create
        [HttpGet, ActionName("Create")]
        public ActionResult CreateByGET(string mod)
        {
            ViewBag.AreCustomersSelectable = (mod == "selectOneCust");
            return View();
        }

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(Customer customer, string submit)
        {
#if DEBUG == true
            switch (submit)
            {
                case "Crear":
                    return RedirectToAction("Index");
                case "Crear y seleccionar":
                    return RedirectToAction("Create", "Transaction", new { customerId = 1 });
                default:
                    return RedirectToAction("Index");
            }
#else
            if (ModelState.IsValid)
            {
                db.Customers.Add(customer);
                db.SaveChanges();
                switch (submit)
                {
                    case "Crear":
                        return RedirectToAction("Index");
                    case "Crear y seleccionar":
                        return RedirectToAction("Create", "Transaction", new { customerId = customer.CustomerID });
                    default:
                        return RedirectToAction("Index");
                }


                
            }


            return View(customer);
#endif
        }
        #endregion
        #region Edit
        //
        // GET: /Customer/Edit/5

        public ActionResult Edit(int id)
        {
#if DEBUG == true
            return View(new Customer
            {
                Country = "Argentina",
                CustomerID = id,
                Email = "mail@mail.com",
                FirstName = "Custy",
                LastName = "Merr",
                Locality = "NoWhereLand",
                MlUser = "MLUser",
                MobilePhone = "54122435",
                Phone = "1234548",
                Province = "NoProvince"

            });

#else
            Customer customer = db.Customers.Find(id);
            return View(customer);
#endif
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(Customer customer)
        {
#if DEBUG == true
            return RedirectToAction("Index");
#endif
            if (ModelState.IsValid)
            {
                db.Entry(customer).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(customer);
        }
        #endregion
        #region Delete
        //
        // GET: /Customer/Delete/5

        public ActionResult Delete(int id)
        {
#if DEBUG == true
            return View(new Customer
            {
                Country = "Argentina",
                CustomerID = id,
                Email = "mail@mail.com",
                FirstName = "Custy",
                LastName = "Merr",
                Locality = "NoWhereLand",
                MlUser = "MLUser",
                MobilePhone = "54122435",
                Phone = "1234548",
                Province = "NoProvince"

            });
#else
            Customer customer = db.Customers.Find(id);
            return View(customer);
#endif
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
#if DEBUG == true
            return RedirectToAction("Index");
#else
            Customer customer = db.Customers.Find(id);
            db.Customers.Remove(customer);
            db.SaveChanges();
            return RedirectToAction("Index");
#endif
        }
        #endregion
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}